<template>
    <div id="content">
        <Header />
        <div class="pack_main">
            <div class="banner" :style="`background-image: url(${$baseUrl+banner_url})`"></div>
            
            <div class="gift-recommend content_main">
                <div class="g-title">
                    <div class="input">
                        <el-input @input="handleSearch" @keyup.enter='searchGift' clearable class="input-with-select" prefix-icon="search" style="height: 38px;" v-model="search_input" :placeholder="$t('gift_title')">
                            <template #append>
                                <el-button @click="searchGift">搜索</el-button>
                            </template>
                        </el-input>
                    </div>
                    <div class="btn btn-red btn-to-mygift">
                        <i class="ic ic-gift"></i><span style="margin: initial;" @click="$router.push('/userCenter/giftPack')">{{ $t('my_gift') }}</span>
                    </div>
                </div>
            </div>
            <div v-if="!isSearchRes">
                <div class="gift-recommend content_main">
                    <div class="g-title">
                        <h4>{{ $t('free_gift_pack') }}</h4>
                    </div>
                    <ul class="list">
                        <li v-for="item in freeGiftList" :key="item.id" v-show="item.gift_type == 0">
                            <a @click="openGiftBox(item)" href="javascript:;" class="gift-box">
                                <div class="img">
                                    <img :src="$baseUrl+item.gift_img">
                                </div>
                                <div class="info">
                                    <h5>{{ item.gift_name }}</h5>
                                    <div class="price-box">
                                        <em>{{ $t('free_receive') }}</em> 
                                        <span class="price-old">{{ $t('price') }}<b>0</b></span>
                                    </div>
                                </div>
                                <div class="hover-bg">
                                    <div class="btn">{{ $t('free_receive') }}</div>
                                    <div class="depict">
                                        <h5>{{ item.title + "：" +item.gift_name }}</h5>
                                        <p>{{ item.gift_content }}</p>
                                    </div>
                                </div>
                            </a>
                        </li>
                        <p v-show="freeGiftList.filter(item=>item.gift_type == 0).length == 0" style="padding: 20px;color: #00ace6;">{{$t('No_gift_packages')}}</p>
                    </ul>
                </div>

                <div class="gift-recommend content_main">
                    <div class="g-title">
                        <h4>{{ $t('integral_gift_pack') }}</h4>
                    </div>
                    <ul class="list">
                        <li v-for="item in freeGiftList" :key="item.id" v-show="item.gift_type == 1">
                            <a @click="openGiftBox(item)" href="javascript:;" class="gift-box">
                                <div class="img">
                                    <img :src="$baseUrl+item.gift_img">
                                </div>
                                <div class="info">
                                    <h5>{{ item.gift_name }}</h5>
                                    <div class="price-box">
                                        <em>{{ $t('integral_redemption') }}</em> 
                                        <span class="price-old">{{ $t('price') }}<b>{{ item.integral }}</b></span>
                                    </div>
                                </div>
                                <div class="hover-bg">
                                    <div class="btn">{{ $t('integral_redemption') }}</div>
                                    <div class="depict">
                                        <h5>{{ item.title + "：" +item.gift_name }}</h5>
                                        <p>{{ item.gift_content }}</p>
                                    </div>
                                </div>
                            </a>
                        </li>
                        <p v-show="freeGiftList.filter(item=>item.gift_type == 1).length == 0" style="padding: 20px;color: #00ace6;">{{$t('No_gift_packages')}}</p>
                    </ul>
                </div>
            </div>


            <GiftAll :detailWapper_farther="changeWap" :freeGiftList="freeGiftList" :searchRes="searchRes" :isSearchRes="isSearchRes" />
        </div>

        <div class="blocker_bg" v-if="detailWapper"></div>
        <div class="blocker" v-if="detailWapper">
            <div class="modal">
                <div class="close" @click="detailWapper = false"></div>
                <div class="pack_detail">
                    <h2>{{ $t('Gift_Introduction') }}</h2>
                    <div class="pack_game">
                        <div class="pack_game_img" :style="`background-image:url(${$baseUrl+giftDetail.icon})`"></div>
                        <div class="pack_game_btn">
                            <div>
                                <button class="play_btn" @click="open_game(freeGiftList)">{{ $t('play_game') }}</button>
                                <button v-if="!isShowCode" class="play_btn" @click="getGift()">{{gift_type?`${giftDetail.integral} ${$t('integral_redemption')}`:`${$t('free_receive')}`}}</button>
                                <button v-if="isShowCode" class="play_btn" @click="copy_code(giftDetail.gift_code)">{{ $t('copy_code') }}</button>
                            </div>
                            <p v-if="isShowCode">{{ $t('gift_code') }}：<b>{{ giftDetail.gift_code }}</b></p> 
                        </div>
                    </div>
                    <div class="pack_detail_item">
                        <h4>{{ $t('Gift_Details') }}</h4>
                        <p>{{ giftDetail.gift_content }}</p>
                    </div>
                    <div class="pack_detail_item">
                        <h4>{{ $t('usage_method') }}</h4>
                        <!-- <p>兑换路径：点击左上角头像-兑换码，输入兑换码去邮箱领取，邮箱会在通关剧情3-2后开启。</p> -->
                         <p>{{ giftDetail.instructions }}</p>
                    </div>
                    <div class="pack_detail_item">
                        <h4>{{ $t('Effective_time') }}</h4>
                        <p>{{giftDetail.start_at}}~{{giftDetail.end_at}}</p>
                    </div>
                    <!-- <div class="pack_detail_item">
                        <h4>友情提示</h4>
                        <p>{{ giftDetail.gift_Note }}</p>
                    </div> -->
                </div>
            </div>
        </div>
        <div class="footer">
            <Footer />
        </div>
        
        <div v-if="isShowLoginStore.isShowLogin">
            <Login :isShowLogin_fn="isShowLogin_fn"/>
        </div>
        
        <BindAccount :visible="visible" :handleClose='handleClose' :getUserInfo="getUserInfo"/>
    </div>
</template>

<script setup>
    // import Login from "@/components/Login/index.vue"
    import Header from "@/components/Header/index.vue"  //头部
    import Footer from "@/components/Footer/index.vue"  //底部
    import GiftAll from "@/components/GiftAll/index.vue"  //底部
    import {ref,reactive, h} from 'vue'
    import axios from '@/utils/api'
    import { useUserInfoStore } from '@/store/modules/user_info/index';
    import { useRouter,useRoute } from 'vue-router'
    import {useGetGameUrl} from '@/hooks/getGameUrl'
    import BindAccount from "@/components/UserCenter/bindAccount/index.vue"
    import {useIsShowLogin} from "@/store/modules/isShowLogin/index.js"

    const isShowLoginStore = useIsShowLogin();
    const isShowLogin_fn = ()=>{
        isShowLoginStore.unShowLogin()
    }
    const router = useRouter()
    const route = useRoute()

    let search_input = ref('')

    const user_infoStore = useUserInfoStore();
    let user_info = reactive(user_infoStore.userInfo)
    const userid = user_infoStore.userInfo.id
    // console.log(userid);
    let giftDetail = reactive({})
    let isShowCode = ref(0)
    let detailWapper = ref(false);
    const changeWap = (type,item)=>{ //供子组件触发打开礼包详情
        detailWapper.value = type
        giftDetail = item
        isShowCode.value = 0
        gift_type = giftDetail.gift_type
    }
    let gift_type = ref('')

    const banner_url = ref('')
    const getBanner = async () =>{
        const banner = await axios.get('/getGiftBannerList')
        banner_url.value = banner.data[0].banner_url
    }
    getBanner()

    const freeGiftList = reactive([])
    const getFreeGiftList = async () =>{
        const list = await axios.get('/getGiftList')
        Object.assign(freeGiftList,list.data.data)
        console.log(freeGiftList);

    
        if(route.params.id){
            const detail = freeGiftList.find(item=>item.id == route.params.id)
            if(detail){
                setTimeout(() => {
                    openGiftBox(detail) 
                }, 300);
            }
            
        }
    }
    getFreeGiftList()

    // const gift
    const openGiftBox = (item)=>{
        detailWapper.value = true
        giftDetail = item
        isShowCode.value = 0
        gift_type = giftDetail.gift_type
        console.log(giftDetail);
    }

    const open_game = async () => {
        const data = await useGetGameUrl(giftDetail.game_id,userid)
        console.log(data.game_url);
        console.log(data);
        if(data.code == 5002 || data.code == 5003 || data.message == 'Token失效'){
            // router.push('/login')
            return
        }
        // localStorage.setItem('game_url',data.game_url)
        const url = router.resolve({ path: '/playgame',query: { id: giftDetail.game_id,game_name: data.game_name,pixel_code:giftDetail.pixel_code } }).href;
        window.open(url)
        
    }

    const getGift = async (giftId)=>{
        const data = await axios.get('/userGetGift?gift_id='+giftDetail.id+'&user_id='+userid)
        console.log(data);
        if(data.code == 200){
            if(lang.value == 'en'){
                ElMessage.success('Received successfully')
            }else{
                ElMessage.success('領取成功')
            }
            isShowCode.value = 1
            giftDetail.gift_code = data.data.gift_code
            getUserInfo()
        }else if(data.code == 7016){
            if(lang.value == 'en'){
                ElMessage.warning('already received it')
            }else{
                ElMessage.warning('已经領取过了')
            }
            
            isShowCode.value = 1
            giftDetail.gift_code = data.data.gift_code
        }else if(data.code == 7021){
            ElMessage.warning(data.message)
            visible.value = true
        }else if(data.code == 7017){
            if(lang.value == 'en'){
                ElMessage.warning('Insufficient remaining inventory of gift packages')
            }else{
                ElMessage.warning('虛寶剩余库存不足')
            }
        }else{
            ElMessage.error(data.message)
        }
    }

    const copy_code = (code) =>{
        if (navigator.clipboard && navigator.clipboard.writeText) {
            // 当前环境支持 clipboard API
            navigator.clipboard.writeText(code).then(() => {
                console.log('复制成功');
                // 使用你的消息提示
                if(lang.value == 'en'){
                    ElMessage.success('Copy successful');
                }else{
                    ElMessage.success('複製成功');
                }
                
            }).catch(err => {
                console.error('复制失败', err);
                ElMessage.error('复制失败');
            });
        } else {
            if(lang.value == 'en'){
                ElMessage.warning('The current environment does not support copying function, please manually copy');
            }else{
                ElMessage.warning('当前环境不支持复制功能，请手动复制');
            }
            
        }
    }

    const userInfo = ref([])
    const getUserInfo = async () =>{
        const data = {
            user_id:user_info.id,
        }
        const list = await axios.post('/getUserInfo',data)
        userInfo.value = list.data
        user_infoStore.setUserinfo(list.data.user_info)
    }

    const visible = ref(false)
	
    const handleClose = () => {
        visible.value = false
    };

    let lang = ref('')
    if(localStorage.getItem('lang') == 'en'){
        lang.value = 'en'
    }else{
        lang.value = 'zh'
    }

    let searchRes = ref([]);
    let isSearchRes = ref(false)
    const searchGift = async ()=>{
        console.log(123);
        if(!search_input.value) return
        const list = await axios.get('/getGiftList?gift_name='+search_input.value)
        searchRes.value = list.data.data
      
        isSearchRes.value = true
        
    }

    const handleSearch = (e)=>{
        console.log(e);
        if(e == ''){
            searchRes.value = []
            isSearchRes.value = false
        }
    }
    
</script>

<style lang="less" scoped>

.pack_main{
    .gift-all .gift-box:after {
        content: '';
        clear: both;
        height: 0;
        display: block;
    }
    min-width: 900px;
    width: 100%;
    .banner {
        width: 100%;
        display: block;
        height: 400px;
        background-repeat: no-repeat;
        background-size: cover;
        // background-image: url(https://static.biligame.net/b-gift/pc/img/banner-index-180126.jpg?v=728781bf.jpg);
    }
    .gift-recommend {
        // min-height: 314px;
        position: relative;
        .g-title {
            padding-top: 90px;
            padding-bottom: 40px;
            position: relative;
            .input{
                position: absolute;
                right: 180px;
                top: 90px;
                height: 38px;
                display: flex;
                width: 320px;
                align-items: center;
                button{
                    cursor: pointer;
                }
            }
            h4 {
                font-size: 24px;
                line-height: 36px;
                color: #333;
                font-weight: 400;
            }
            h4:before {
                content: '';
                float: left;
                margin-top: 6px;
                margin-right: 16px;
                z-index: 2;
                background: #00ace6;
                width: 4px;
                height: 24px;
            }
            .btn {
                position: absolute;
                right: 9px;
                top: 90px;
                color: #00ace6;
                background-color: #fff;
                border: 1px solid #00ace6;
                background-repeat: no-repeat;
                transition: all .2s;
                display: flex;
                height: 36px;
                line-height: 34px;
                padding: 0 22px 0 15px;
                font-size: 14px;
                cursor: pointer;
                text-align: center;
                text-decoration: none;
                text-decoration-line: none;
                text-decoration-thickness: initial;
                text-decoration-style: initial;
                text-decoration-color: initial;
                border-radius: 4px;
                .ic-gift {
                    background-image: url();
                    background-position: center;
                    width: 20px;
                    height: 20px;
                    margin: 8px;
                    display: inline-block;
                }
            }
            .btn:hover {
                color: #fff;
                background-color: #00ace6;
                .ic-gift {
                    background-image: url();
                }
            }
        }
        .list{
            li {
                list-style: none;
                a{
                    text-decoration: none;
                }
                font-size: 14px;
                width: 20%;
                height: 200px;
                display: inline-block;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                vertical-align: middle;
                margin-bottom: 10px;
                .gift-box {
                    display: block;
                    width: 210px;
                    height: 168px;
                    margin: 0 auto;
                    background-color: #fff;
                    -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, .1);
                    box-shadow: 0 1px 2px rgba(0, 0, 0, .1);
                    border-radius: 4px;
                    -webkit-transition: all .3s;
                    transition: all .3s;
                    position: relative;
                    overflow: hidden;

                    .img {
                        position: relative;
                        display: block;
                        width: 100%;
                        height: 105px;
                        overflow: hidden;
                        background-repeat: no-repeat;
                        background-position: center;
                        background-color: #e5e9ef;
                        -webkit-border-top-left-radius: 4px;
                        -webkit-border-top-right-radius: 4px;
                        border-top-left-radius: 4px;
                        border-top-right-radius: 4px;
                        -webkit-transition: all .3s;
                        transition: all .3s;
                        img {
                            display: block;
                            width: 100%;
                            height: 100%;
                            position: absolute;
                            left: 50%;
                            top: 50%;
                            -webkit-transform: translate(-50%, -50%);
                            -ms-transform: translate(-50%, -50%);
                            transform: translate(-50%, -50%);
                        }
                    }
                    .info {
                        padding: 10px 10px 15px;
                        line-height: 1;
                        h5 {
                            font-size: 16px;
                            color: #333;
                            font-weight: 400;
                            line-height: 18px;
                            margin-bottom: 8px;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                        }
                        .price-box {
                            font-size: 12px;
                            em {
                                color: #fb7299;
                                font-size: 12px;
                                line-height: 1;
                                display: inline-block;
                            }
                            .price-old {
                                float: right;
                                height: 12px;
                                font-size: 12px;
                                line-height: 1;
                                color: #99a2aa;
                                padding: 0 4px;
                                position: relative;
                                vertical-align: bottom;
                                b {
                                    font-weight: 700;
                                    font-family: Arial;
                                    font-size: 12px;
                                    display: inline-block;
                                    vertical-align: middle;
                                    position: relative;
                                    margin-left: 5px;
                                }
                            }
                            // .price-old:before {
                            //     content: '';
                            //     position: absolute;
                            //     width: 100%;
                            //     height: 1px;
                            //     background-color: #99a2aa;
                            //     bottom: 4px;
                            //     left: 0;
                            // }
                        }
                    }
                    .hover-bg {
                        display: none;
                        position: absolute;
                        left: 0;
                        top: 0;
                        width: 100%;
                        height: 100%;
                        text-align: center;
                        background-image: url(@/static/images/depict-bg.png);
                        background-repeat: repeat;
                        background-position: center;
                        background-size: 110% 100%;

                        .btn {
                            font-size: 14px;
                            border-radius: 15px;
                            border: 1px solid #fff;
                            // width: 92px;
                            padding: 0 10px;
                            height: 32px;
                            line-height: 32px;
                            margin-top: 75px;
                            color: #fff;
                            display: inline-block;
                            cursor: pointer;
                            text-align: center;
                            text-decoration: none;
                        }
                        .depict {
                            position: absolute;
                            left: 0;
                            bottom: 0;
                            width: 100%;
                            height: 85px;
                            color: #fff;
                            h5 {
                                font-size: 16px;
                                margin: 0 auto;
                                text-align: center;
                                margin-top: 15px;
                                overflow: hidden;
                                text-overflow: ellipsis;
                                white-space: nowrap;
                                padding: 0 5px;
                            }
                            p {
                                text-align: center;
                                margin-top: 10px;
                                padding: 0 20px;
                                height: 40px;
                                overflow: hidden;
                                word-wrap: break-word;
                                white-space: nowrap;
                                text-overflow: ellipsis;
                                font-size: 13px;
                            }
                        }
                    }
                }
                .gift-box:hover  {
                    height: 190px;
                    transform: scale(1.1);
                    .hover-bg{
                        display: block;
                    }
                }
            }
        }
    }

    

}

.blocker_bg{
    position: fixed;
    width: 100%;
    height: 100vh;
    background-color: rgba(0, 0, 0, .3);
    top: 0;
}
.blocker_show{
    transform: translate(-50%,-50%) scale(1) !important;
}

.blocker{
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 100%;
    height: 100%;
    z-index: 2000 !important;
    transition: all .3s;
    animation: popIn 0.3s ease-out;
    .modal{
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        width: 500px;
        background-color: #fff;
        box-shadow: 1px 3px 20px rgba(0, 0, 0, .3);
        padding: 30px;
        border-radius: 12px;
        .pack_detail{
            h2{
                text-align: center;
                color: #fb9817;
            }
            .pack_detail_item{
                margin-top: 15px;
                p{
                    color: #999;
                    font-size: 14px;
                    margin-top: 5px;
                }
            }
            .pack_game{
                display: flex;
                align-items: center;
                margin-top: 10px;
                .pack_game_img{
                    width: 80px;
                    height: 80px;
                    // background-image: url(https://www.91easyplay.com/data/upload/game/2023-12-19/65811fd405fe7.png);
                    background-size: cover;
                    margin: initial;
                    border-radius: 12px;
                }
                .play_btn{
                    padding: 10px 20px;
                    background-color: #00ace6;
                    color: #fff;
                    border: none;
                    border-radius: 5px;
                    height: 37px;
                    margin: initial;
                    margin-left: 20px;
                    
                }
                .pack_game_btn{
                    height: 66px;
                    margin: initial;
                    p{
                        margin-left: 20px;
                        margin-top: 10px;
                        font-size: 14px;
                        color: #999;
                        span{
                            margin-left: 10px;
                            cursor: pointer;
                        }
                    }
                    button{
                        cursor: pointer;
                    }
                }
            }
        }
    }
}

</style>